package com.example.controller;

import com.example.Result;
import com.example.annotation.AsyncMessageHandler;
import com.example.entity.Admin;
import com.example.service.AdminService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin")
@Tag(name = "管理员管理")
public class AdminController {
    @Autowired
    private AdminService adminService;

    @PostMapping("/login")
    @Operation(summary = "管理员登录")
    @AsyncMessageHandler(queue = "admin.login.queue")
    public Result login(@RequestBody Admin admin) {
//        return adminService.login(admin.getUsername(), admin.getPassword());
        return Result.success(adminService.login(admin.getUsername(), admin.getPassword()));
    }

    @PostMapping
    @Operation(summary = "新增管理员")
    @PreAuthorize("hasRole('SUPER_ADMIN')")
    @AsyncMessageHandler(queue = "admin.register.queue")
    public boolean saveAdmin(@RequestBody Admin admin) {
        return adminService.saveAdmin(admin);
    }

    @PutMapping
    @Operation(summary = "修改管理员")
    @PreAuthorize("hasRole('SUPER_ADMIN')")
    @AsyncMessageHandler(queue = "admin.update.queue")
    public boolean updateAdmin(@RequestBody Admin admin) {
        return adminService.updateAdmin(admin);
    }

    @PutMapping("/{id}/status")
    public boolean updateStatus(@PathVariable Long id, @RequestParam Integer status) {
        return adminService.updateStatus(id, status);
    }
} 